<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Post</title>
</head>
<body>
<%@ page import="tuna.Process" %>
<%@ page import="tuna.DBConnection" %>
<%@ page import="java.sql.ResultSet;" %>
<%
	String day = request.getParameter("day");
	String us = request.getParameter("us");
	String role = request.getParameter("role");
	String id = request.getParameter("id");
	String userId = request.getParameter("userId");
	String userName = request.getParameter("userName");
	
	if(Process.validate(day, us, role, id)) {
		String parameters = "?day=" + day + "&us=" + us + "&role=" + role + "&id=" + id;
		String msgParameters = parameters + "&senderId=" + userId + "&senderUtorId=" + userName;
%>
	<table align="center" style="font-family: arial; width: 480px;">
		<tr>
			<td>
				Posts written by <a href="/tuna/composeMessage.jsp<%= msgParameters %>"><%= userName %></a>
			</td>
			<td>
				<input type="button" value="Write Post" onclick="location.href='/tuna/writePost.jsp<%= parameters %>'" />
			</td>
		</tr>
		<tr>
			<td colspan=2>
				&nbsp;
			</td>
		</tr>
		<tr>
			<td colspan=2>
				<table>
					<tr>
						<td>					
							Detail
						</td>
						<td>
							Time
						</td>
					</tr>
				<% 
					//Create DB connection & connect
				    DBConnection dbCon = new DBConnection("localhost:3306", "tuna", "root", "asdf1234");
				    dbCon.connectDB();

				    //Retrieve result set
				    String query = "select detail, writtenAt from tuna.postRecord where userId=" + userId +
				    " order by writtenAt desc";
				    
				    ResultSet posts = dbCon.runQuery(query);	
				    
				    while(posts.next()) {
						String detail = posts.getString(1);
						String writtenAt = posts.getString(2);
				%>		
					<tr>
						<td>
							<%= detail %>
						</td>
						<td>
							<%= writtenAt %>
						</td>
					</tr>
				<%	
				    }
				    
				    dbCon.disconnectDB();
				%>
				</table>
			</td>
		</tr>
	</table>
<% 
	}
	else {
		response.sendRedirect("/tuna/login.jsp");
	}
%>
</body>
</html>